"use strict";
honey.def("lib:jquery", function(e) {
	var t = {};
	t.lastModify = "2015-01-06 21:01:07", t.ver = "v1.0", t.name = "鑺掓灉TV瑙嗛鎾斁鍣ㄥ箍鍛婃彃浠�", t.ading = !0, t.padading = !0, t.windowStaus = 0;
	$(window), $("body");
	var n = navigator.userAgent,
		i = (/iPhone/i.test(n), /iPod/i.test(n), /iPad/i.test(n)),
		a = (/Android/i.test(n), []),
		r = 0,
		d = 0,
		o = 0,
		c = 0,
		l = !1,
		s = {};
	s.duration = 0, s.adObjects = [], s.ids = "";
	var v = [],
		u = {
			cookieGet: function(e) {
				var t = new RegExp("(?:^|;+|\\s+)" + e + "=([^;]*)"),
					n = document.cookie.match(t);
				return n ? n[1] : ""
			},
			encodeTk2: function(e) {
				return btoa(e).replace(/\+/g, "_").replace(/\//g, "~").replace(/=/g, "-").split("").reverse().join("")
			},
			decodeTK2: function(e) {
				return atob(e.split("").reverse().join("").replace(/-/g, "=").replace(/~/g, "/").replace(/_/g, "+"))
			},
			parseTk2: function(e) {
				var t = {};
				return e.split("|").forEach(function(e) {
					var n = e.split("=");
					n && n.length && (t[n[0]] = n[1])
				}), t
			},
			sendLog: function(e, t) {
				if ($.isArray(e)) for (var n = 0; n < e.length; n++) {
					var i = new Image,
						a = e[n];
					t && (a = u.replaceDefines(a, t)), i.src = a
				}
			},
			replaceDefine: function(e, t, n) {
				return e.replace(t, n)
			},
			replaceDefines: function(e, t) {
				for (var n in t) e = u.replaceDefine(e, t[n].pattern, t[n].value);
				return e
			}
		};
	window.getGUID = function() {
		var e = function(e) {
				var t = location.search,
					n = {};
				if (-1 != t.indexOf("?")) for (var i = 0, a = t.substr(1).split("&"); i < a.length; i += 1) n[a[i].split("=")[0]] = unescape(a[i].split("=")[1]);
				var r = n[e.toLowerCase()];
				return void 0 === r ? "" : r
			}("from"),
			t = function(e) {
				var t = new RegExp("(?:^|;+|\\s+)" + e + "=([^;]*)"),
					n = document.cookie.match(t);
				return n ? n[1] : ""
			},
			n = document.referrer,
			i = "" == t("MQGUID") ? t("__MQGUID") : t("MQGUID");
		return "" == n && "wb" == e && (n = "http://www.weibo.com"), i + "|" + n
	};
	var p = $("#mgtv-video-wrap"),
		g = $(".mgtv-video-income"),
		m = function(e) {
			g.hide(), $("[v-mvp-ad=player_ad_inner]").hide(), p.find(".loading-box").hide(), console.log("gotoPlay:", e, s.ids, 0, s.duration, s.adObjects.length || 0), t.ading = !1, t.padading = !1, $(document).trigger("player.adend", {
				adId: s.ids,
				adType: 0,
				adTime: s.duration,
				adNum: s.adObjects.length || 0,
				endType: e
			})
		};

	function f(e) {
		var t = $("[v-mvp-ad=player_ad_count] i");
		t.html(e);
		var n = setInterval(function() {
			var e = parseInt(t.html());
			e <= 0 ? clearInterval(n) : t.html(e - 1)
		}, 1e3)
	}
	function h() {
		p.find(".loading-box").hide(), a[c] = g[c % 2].duration, a[c], g[c % 2].removeEventListener("loadedmetadata", h, !0)
	}
	function O() {
		o = g[d].currentTime, function(e, n) {
			var i = s.adObjects[e].impress;
			if (i && i.length) for (var a = 0; a < i.length; a++) if (n > i[a].t) return u.sendLog(i[a].ul, [{
				pattern: /\[TIME\]/,
				value: o
			}, {
				pattern: /\[STATUS\]/,
				value: t.windowStaus
			}]), void i.splice(a, 1)
		}(r, o), a[r] - o < 5 && !l && (l = !0, w(r + 1))
	}
	function w(e) {
		var t = e % 2;
		e < s.adObjects.length && (c++, g[t].preload = "auto", g[t].src = s.adObjects[e].mediaFileUrl, g[t].addEventListener("loadedmetadata", h, !0), console.log("姝ｅ湪鎾斁绗�" + (e + 1) + "鍓嶈创鐗囧箍鍛�, 鍏�" + s.adObjects.length + "涓�"), console.log("鍓嶈创鐗囧箍鍛婂湴鍧€涓�: " + s.adObjects[e].mediaFileUrl))
	}
	function I() {
		var e = Number(!d);
		l = !1, ++r < s.adObjects.length ? ($(".mgtv-video-income:eq(" + e + ")").show(), $(".mgtv-video-income:eq(" + d + ")").hide(), g[d].pause(), g[d].removeEventListener("timeupdate", O, !0), g[d].removeEventListener("ended", I, !0), g[d].removeEventListener("error", y, !0), g[e].addEventListener("timeupdate", O, !0), g[e].addEventListener("ended", I, !0), g[e].addEventListener("error", y, !0), g[e].play(), $("[v-mvp-ad=player_ad_link]").attr("href", s.adObjects[r].clickThrough), d = e) : (g[d].removeEventListener("ended", I, !0), m("switch next"))
	}
	function y(e) {
		console.log("骞垮憡鏁版嵁婧愬嚭閿欙細", e), function(e, n) {
			var i = s.adObjects[index].linkTrack,
				a = null;
			if ("err" == e && (a = [{
				pattern: /\[ERRORCODE\]/,
				value: n.code
			}, {
				pattern: /\[ERRORMSG\]/,
				value: n.msg
			}, {
				pattern: /\[ERRORURL\]/,
				value: n.url
			}, {
				pattern: /\[STATUS\]/,
				value: t.windowStaus
			}]), i && i.length) for (var r = 0; r < i.length; r++) e == i[r].d && u.sendLog(i[r].ul, a)
		}("err", {
			code: e.currentTarget.error.code,
			msg: evt.currentTarget.error.message,
			url: s.adObjects[r].mediaFileUrl
		}), l ? I() : (l = !0, w(r + 1), I())
	}
	function b() {
        var e = $("#mgtv-video-player")[0];
        s.adObjects.length = 0;
		s.adObjects.length > 0 ? (v = s.adObjects.shift(), e.src = v.mediaFileUrl, e.play()) : (e.removeEventListener("ended", b, !0), m("no ad"))
	}
	function E() {
		$("[v-mvp-ad=player_ad_inner]").show(), f(s.duration), $("#mgtv-video-player")[0].removeEventListener("playing", E, !0), $("div[node-type=loading-box]").hide()
	}
	t.initIpad = function() {
		$("#mgtv-video-player")[0].play()
	}, t.AdPlayer = function(e) {
		p = e.boxId && "" !== e.boxId ? $(e.boxId) : $("#mgtv-video-wrap"), g = $(".mgtv-video-income")
	}, t.fetchAd = function(e, t, n) {
		var a, r = void 0,
			d = this;
		e = e || {
			type: "front"
		}, r = {
			m: {
				p: i ? 4482 : 4388,
				ptype: e.type,
				aids:  [],
				pu: encodeURIComponent(window.location.href)
			},
			u: {
				passport: u.cookieGet("uuid") || "",
				isContinue: VIDEOINFO.isAutoPlay ? 1 : 0,
				cxid: STK.$.getCxid("cxid") || "",
				ck: STK.$.globalStorage() || "",
				sid: STK.$.sessionid() || ""
			},
			c: {
				type: i ? 23 : 1,
				version: "",
				os: i ? "ipad-" + navigator.userAgent.match(/OS\b (.*)\b like/)[1].replace(/_/g, ".").replace("-", " ") : "pc",
				rs: window.innerWidth + "*" + window.innerHeight,
				mac: "",
				ua: navigator.userAgent,
				lt: 0,
				ts: (new Date).getTime(),
				cw: window.innerWidth,
				ch: window.innerHeight,
				cori: window.orientation,
				sd: window.devicePixelRatio
			}
		};
		var o = {
			url: encodeURIComponent(window.location.href),
			vip: "1" == u.cookieGet("vipStatus") ? 1 : 0,
			uname: u.cookieGet("uuid") || "",
			ucode: ""
		};
		if ("front" == e.type) {
			var c = {},
				l = "did=|uid=" + window.getGUID() + "|vip=" + o.vip + "|pno=1030|ver=|clit=" + Math.floor((new Date).getTime() / 1e3);
			c.tk2 = u.encodeTk2(l), t && (c.pm2 = t.pm2), r.atc = c
		}
		a = {
			v: {
				id: parseInt(VIDEOINFO.vid),
				hid: parseInt(VIDEOINFO.cid),
				rid: VIDEOINFO.rid,
				url: VIDEOINFO.url,
				on_date: VIDEOINFO.year,
				clip_type: VIDEOINFO.clip_type,
				vtt: "200" == VIDEOINFO.user.purview ? parseInt(VIDEOINFO.info.duration, 10) : parseInt(VIDEOINFO.info.trialtime, 10),
				ispreview: "200" == VIDEOINFO.user.purview ? 0 : 1,
				ispay: "0" == VIDEOINFO.info.paymark ? 0 : 1,
				vip: o.vip,
				uname: o.uname,
				ucode: o.ucode
			}
		}, $.ajax({
			url: "//web.da.mgtv.com/m/player?p=" + JSON.stringify(r) + "&v=" + JSON.stringify(a) + "&_type_object=p,v&callback=?",
			type: "GET",
			dataType: "json",
			timeout: 5e3,
			success: function(e) {
                var s= {
                    data:null,
                    ets:{
                        atc:{
                            pm2:e.v.ets.atc.pm2,
                            tk2:e.v.ets.atc.tk2
                        }
                    }
                }
                e.v = s;
				n && n(e), d.parseAd(e)
			},
			complete: function(e, t) {
				"timeout" == t && m("fail api")
			}
		})
	}, t.parseAd = function(e) {

		if (s.duration = 0, s.adObjects = [], s.ids = "", e) {
			var n = e.v.data;
			e.v.ets && e.v.ets.atc && (s.atc = e.v.ets.atc || {}), n ? (console.log(n.length, "ad.length"), $.each(n, function(e, t) {
				var n = t.creative[0],
					i = {
						as: t.as,
						isAd: !0,
						adType: t.tp,
						id: t.id,
						mediaFileType: n.mf.tp,
						mediaFileUrl: n.mf.url,
						duration: parseInt(n.mf.dr, 10),
						source: n.mf.url,
						width: parseInt(n.mf.w),
						height: parseInt(n.mf.h),
						impress: t.ts,
						link: t.cls,
						linkTrack: t.es,
						clickThrough: t.vc.length ? t.vc[0].ul : ""
					};
				"front" == i.adType && (s.ids += i.id + "_"), s.duration += i.duration, s.adObjects.push(i)
			}), i ?
			function() {
				$(".mgtv-video-income").hide(), $("#mgtv-video-player").show();
				var e = $("#mgtv-video-player")[0];
				s.adObjects.length > 0 ? (v = s.adObjects.shift(), $("[v-mvp-ad=player_ad_link]").attr("href", v.clickThrough), e.src = v.mediaFileUrl, e.addEventListener("ended", b, !0), e.addEventListener("playing", E, !0)) : b()
			}() : ($("[v-mvp-ad=player_ad_link]").attr("href", s.adObjects[r].clickThrough), console.log("clickThrough", s.adObjects[r].clickThrough), $("#a_box").on("click", function() {
				var e = s.adObjects[r];
				e && (e.link && e.link.length && u.sendLog(s.adObjects[r].link[0].ul, [{
					pattern: /\[TIME\]/,
					value: o
				}, {
					pattern: /\[STATUS\]/,
					value: t.windowStaus
				}]), e.clickThrough && window.open(s.adObjects[r].clickThrough))
			}), g[0].preload = "auto", g[0].src = s.adObjects[c].mediaFileUrl, g[0].addEventListener("loadedmetadata", h, !0), g[0].addEventListener("timeupdate", O, !0), g[0].addEventListener("ended", I, !0), g[0].addEventListener("error", y, !0), g[0].play(), f(s.duration), $("[v-mvp-ad=player_ad_inner]").show(), $("div[node-type=loading-box]").hide())) : m("no ad")
		} else m("fail data")
	}, e.INCOME_VIDEO = t
});